package com.ihighest.app.infox.activity;

import java.util.ArrayList;

import org.json.JSONException;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.ChoosePlantsAdapter;
import com.ihighest.app.infox.adapter.LableBindingAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.CheckNetWork;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

/**
 * 选择作物界面
 * 
 * @author lkx Created at 2015-11-24 下午3:27:42
 * @version 1.0
 */
public class ChoosePlantsActivity extends BaseActivity {
	private ActivityTitle title;
	private ListView lisView;
	private ChoosePlantsAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.infox_activity_chooseplants);

		initView();

	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-15 下午2:49:38
	 */
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.choosePlantTitle);
		title.setTitle("我感兴趣");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("完成");

		lisView = (ListView) findViewById(R.id.choosePlantListview);

		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				gettagsData();

			}
		});
		title.titleBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				gettagsData();
			}
		});
	}

	/**
	 * 获取已登录会员绑定数据
	 */
	private void gettagsData() {
		Api.get("me/info", ApiParam.create(), new ApiCallback(this) {

			@Override
			public void handleSuccess(ApiResult result) {

				try {

					SharedPreferencesKit.putJsonArray(getApplicationContext(), Const.TAGS, result.getData().getJSONArray("tags"));
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
				}
				finish();
			}

			@Override
			public void handleFailure(Exception e, String msg) {

				finish();
			}
		}, this);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		initData();
		lisView.setAdapter(adapter);

	}

	/**
	 * 获取标签数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-15 下午7:36:52
	 */
	private void initData() {
		if (CheckNetWork.isConnected(ChoosePlantsActivity.this)) {
			// 如果网络正常
			Api.data("tag_recommend_list.json", new ApiCallback(this, true) {

				@Override
				public void handleSuccess(ApiResult result) {
					if (adapter == null) {
						adapter = new ChoosePlantsAdapter(ChoosePlantsActivity.this, result.getDatas());
					}
					lisView.setAdapter(adapter);
					adapter.notifyDataSetChanged();
				}

				@Override
				public void handleFailure(Exception e, String msg) {
					// TODO Auto-generated method stub

				}
			}, this);

		}

	}
}
